package com.qqzone.controller;

import com.qqzone.pojo.Topic;
import com.qqzone.pojo.UserBasic;
import com.qqzone.service.TopicService;

import java.util.Date;
import java.util.List;

/**
 * @author Yang
 * @creat 18:26 2022/8/26
 * @sice 1.8
 */

public class TopicController {
    private TopicService topicService;

    public String topicDetail(Integer id, javax.servlet.http.HttpSession session) {
        // 得在 topicService.getTopicById 初始化出 replyList
        Topic topic = topicService.getTopicById(id);

        session.setAttribute("topic", topic);
        return "frames/detail";
    }

    public String delTopic(Integer topicId, javax.servlet.http.HttpSession session) {
        topicService.delTopic(topicId);
        return "redirect:topic.do?operate=getTopicList";
    }

    public String getTopicList(javax.servlet.http.HttpSession session) {
        UserBasic userBasic = (UserBasic) session.getAttribute("friend");

        List<Topic> topicList = topicService.getTopicListByUserId(userBasic.getId());

        userBasic.setTopicList(topicList);

        session.setAttribute("friend", userBasic);

        return "frames/main";
    }

    public String addTopic(String title, String content, javax.servlet.http.HttpSession session) {
        UserBasic userBasic = (UserBasic) session.getAttribute("userBasic");
        // 新增回复列表为null
        Topic topic = new Topic(title, content, new Date(), userBasic);
        topicService.addTopic(topic);
        return "redirect:topic.do?operate=getTopicList";
    }
}
